/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.report;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import weblogic.descriptor.descriptorgen.interfacegen;

import com.sinodata.bsm.center.cache.*;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.vo.*;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author zhangchao
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-00-00 *午00:00:00          zhangchao        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */

public class HostHealthReportAction extends BaseAction {

    private static final Long APP_RESTYPE = 1105050000000L;

    @Autowired
    private ResCache resCache;
    @Autowired
    private ResGroupCache resGroupCache;
    @Autowired
    private MotiResRelCache motiResRelCache;

    /**
     * 应用系统健康报表
     * @return
     */
    public String hostHealthReport() {

        List<Res> appList = resCache.getByResType(APP_RESTYPE);//系统

        JSONObject json = new JSONObject();
        JSONArray data = new JSONArray();
        JSONObject obj = new JSONObject();
        for (int i = 0; i < appList.size(); i++) {
            int hosttotal = 0; //主机总数
            int hostnormal = 0; //正常主机数
            int dtotal = 0; //数据库总数
            int dnormal = 0;
            int mtotal = 0; //中间件
            int mnormal = 0;
            int vtotal = 0; //虚拟化
            int vnormal = 0;
            int ototal = 0; //其他、自定义脚本类
            int onormal = 0;
            int ttotal = 0; //进程
            int tnormal = 0;
            int ptotal = 0; //端口
            int pnormal = 0;
            int ftotal = 0; //文件
            int fnormal = 0;
            int stotal = 0; //其他、SQL查询
            int snormal = 0;
            int itotal = 0; //接口
            int inormal = 0;
            //资源组
            List<ResGroup> resGroupList = resGroupCache.findByResId(appList.get(i).getId().longValue());
            if (resGroupList != null) {
                for (ResGroup group : resGroupList) {
                    if (group.getContainType() == null)
                        continue;
                    else if (group.getContainType() == 2000L) {
                        //主机
                        long gid = group.getId();
                        //资源组里所有资源
                        List<MotiResRel> relList = motiResRelCache.findByResGroupId(gid);
                        if (relList != null) {
                            for (int j = 0; j < relList.size(); j++) {
                                Res host = resCache.get(relList.get(j).getResId());
                                if (host != null) {
                                    hosttotal++;
                                    if (host.getHealthStatus() == 1)
                                        hostnormal++;
                                }
                            }
                        }
                    } else if (group.getContainType() == 2100L) {
                        //数据库
                        long gid = group.getId();
                        List<MotiResRel> relList = motiResRelCache.findByResGroupId(gid);
                        if (relList != null) {
                            for (int j = 0; j < relList.size(); j++) {
                                Res host = resCache.get(relList.get(j).getResId());
                                if (host != null) {
                                    dtotal++;
                                    if (host.getHealthStatus() == 1)
                                        dnormal++;
                                }
                            }
                        }
                    } else if (group.getContainType() == 2200L) {
                        //中间件
                        long gid = group.getId();
                        List<MotiResRel> relList = motiResRelCache.findByResGroupId(gid);
                        if (relList != null) {
                            for (int j = 0; j < relList.size(); j++) {
                                Res host = resCache.get(relList.get(j).getResId());
                                if (host != null) {
                                    mtotal++;
                                    if (host.getHealthStatus() == 1)
                                        mnormal++;
                                }
                            }
                        }
                    } else if (group.getContainType() == 2300L) {
                        //虚拟化
                        long gid = group.getId();
                        //资源组里所有资源
                        List<MotiResRel> relList = motiResRelCache.findByResGroupId(gid);
                        if (relList != null) {
                            for (int j = 0; j < relList.size(); j++) {
                                Res host = resCache.get(relList.get(j).getResId());
                                if (host != null) {
                                    vtotal++;
                                    if (host.getHealthStatus() == 1)
                                        vnormal++;
                                }
                            }
                        }
                    } else if (group.getContainType() == 1050L) {
                        //其他、自定义脚本
                        long gid = group.getId();
                        List<MotiResRel> relList = motiResRelCache.findByResGroupId(gid);
                        if (relList != null) {
                            for (int j = 0; j < relList.size(); j++) {
                                Res host = resCache.get(relList.get(j).getResId());
                                if (host != null) {
                                    ototal++;
                                    if (host.getHealthStatus() == 1)
                                        onormal++;
                                }
                            }
                        }
                    } else if (group.getContainType() == 1010L) {
                        //进程
                        long gid = group.getId();
                        List<MotiResRel> relList = motiResRelCache.findByResGroupId(gid);
                        if (relList != null) {
                            for (int j = 0; j < relList.size(); j++) {
                                Res host = resCache.get(relList.get(j).getResId());
                                if (host != null) {
                                    ttotal++;
                                    if (host.getHealthStatus() == 1)
                                        tnormal++;
                                }
                            }
                        }
                    } else if (group.getContainType() == 1020L) {
                        //端口
                        long gid = group.getId();
                        List<MotiResRel> relList = motiResRelCache.findByResGroupId(gid);
                        if (relList != null) {
                            for (int j = 0; j < relList.size(); j++) {
                                Res host = resCache.get(relList.get(j).getResId());
                                if (host != null) {
                                    ptotal++;
                                    if (host.getHealthStatus() == 1)
                                        pnormal++;
                                }
                            }
                        }
                    } else if (group.getContainType() == 1030L) {
                        //文件
                        long gid = group.getId();
                        List<MotiResRel> relList = motiResRelCache.findByResGroupId(gid);
                        if (relList != null) {
                            for (int j = 0; j < relList.size(); j++) {
                                Res host = resCache.get(relList.get(j).getResId());
                                if (host != null) {
                                    ftotal++;
                                    if (host.getHealthStatus() == 1)
                                        fnormal++;
                                }
                            }
                        }
                    } else if (group.getContainType() == 1040L) {
                        //其他、SQL查询
                        long gid = group.getId();
                        List<MotiResRel> relList = motiResRelCache.findByResGroupId(gid);
                        if (relList != null) {
                            for (int j = 0; j < relList.size(); j++) {
                                Res host = resCache.get(relList.get(j).getResId());
                                if (host != null) {
                                    stotal++;
                                    if (host.getHealthStatus() == 1)
                                        snormal++;
                                }
                            }
                        }
                    } else if (group.getContainType() == 100L) {
                        //接口
                        long gid = group.getId();
                        List<MotiResRel> relList = motiResRelCache.findByResGroupId(gid);
                        if (relList != null) {
                            for (int j = 0; j < relList.size(); j++) {
                                Res host = resCache.get(relList.get(j).getResId());
                                if (host != null) {
                                    itotal++;
                                    if (host.getHealthStatus() == 1)
                                        inormal++;
                                }
                            }
                        }
                    }
                }
            }
            obj.element("1", appList.get(i).getName());
            obj.element("2", hosttotal);
            obj.element("3", hostnormal);
            obj.element("4", hosttotal - hostnormal);
            obj.element("5", dtotal);
            obj.element("6", dnormal);
            obj.element("7", dtotal - dnormal);
            obj.element("8", mtotal);
            obj.element("9", mnormal);
            obj.element("10", mtotal - mnormal);
            obj.element("11", vtotal);
            obj.element("12", vnormal);
            obj.element("13", vtotal - vnormal);
            obj.element("14", ototal);
            obj.element("15", onormal);
            obj.element("16", ototal - onormal);
            obj.element("17", ttotal);
            obj.element("18", tnormal);
            obj.element("19", ttotal - tnormal);
            obj.element("20", ptotal);
            obj.element("21", pnormal);
            obj.element("22", ptotal - pnormal);
            obj.element("23", ftotal);
            obj.element("24", fnormal);
            obj.element("25", ftotal - fnormal);
            obj.element("26", stotal);
            obj.element("27", snormal);
            obj.element("28", stotal - snormal);
            obj.element("29", itotal);
            obj.element("30", inormal);
            obj.element("31", itotal - inormal);
            data.add(obj);
        }
        json.element("size", appList.size());
        json.element("data", data);

        Struts2Utils.renderText(json.toString());

        return null;
    }

    public String hostHealthChart() {
        List<Res> appList = resCache.getByResType(APP_RESTYPE);//系统
        String chartid = request.getParameter("chartid");
        JSONObject json = new JSONObject();
        JSONObject obj1 = new JSONObject();
        JSONObject obj2 = new JSONObject();
        JSONObject obj3 = new JSONObject();
        JSONObject obj4 = new JSONObject();
        JSONObject obj5 = new JSONObject();
        JSONArray obj = new JSONArray();
        JSONObject data = new JSONObject(); //名称
        JSONObject data1 = new JSONObject(); //总数
        JSONObject data2 = new JSONObject(); //正常
        JSONObject data3 = new JSONObject(); //异常
        JSONArray jname = new JSONArray();
        JSONArray jtotal = new JSONArray();
        JSONArray jnormal = new JSONArray();
        JSONArray ja = new JSONArray();
        for (int i = 0; i < appList.size(); i++) {
            int btotal = 0; //总数
            int bnormal = 0; //正常数

            List<ResGroup> resGroupList = resGroupCache.findByResId(appList.get(i).getId().longValue());
            if (resGroupList != null) {
                for (ResGroup group : resGroupList) {
                    if (chartid == null || chartid.equals("1")) {
                        if (group.getContainType() == null)
                            continue;
                        else if (group.getContainType() == 2000L || group.getContainType() == 2100L || group.getContainType() == 2200L || group.getContainType() == 2300L || group.getContainType() == 1050L) {
                            //主机
                            long gid = group.getId();
                            //资源组里所有资源
                            List<MotiResRel> relList = motiResRelCache.findByResGroupId(gid);
                            if (relList != null) {
                                for (int j = 0; j < relList.size(); j++) {
                                    Res host = resCache.get(relList.get(j).getResId());
                                    if (host != null) {
                                        btotal++;
                                        if (host.getHealthStatus() == 1)
                                            bnormal++;
                                    }
                                }
                            }
                        }
                    } else if (chartid.equals("2")) {
                        if (group.getContainType() == null)
                            continue;
                        else if (group.getContainType() == 1010L || group.getContainType() == 1020L || group.getContainType() == 1030L || group.getContainType() == 1040L) {
                            //主机
                            long gid = group.getId();
                            //资源组里所有资源
                            List<MotiResRel> relList = motiResRelCache.findByResGroupId(gid);
                            if (relList != null) {
                                for (int j = 0; j < relList.size(); j++) {
                                    Res host = resCache.get(relList.get(j).getResId());
                                    if (host != null) {
                                        btotal++;
                                        if (host.getHealthStatus() == 1)
                                            bnormal++;
                                    }
                                }
                            }
                        }
                    } else if (chartid.equals("3")) {
                        if (group.getContainType() == null)
                            continue;
                        else if (group.getContainType() == 100L) {
                            //主机
                            long gid = group.getId();
                            //资源组里所有资源
                            List<MotiResRel> relList = motiResRelCache.findByResGroupId(gid);
                            if (relList != null) {
                                for (int j = 0; j < relList.size(); j++) {
                                    Res host = resCache.get(relList.get(j).getResId());
                                    if (host != null) {
                                        btotal++;
                                        if (host.getHealthStatus() == 1)
                                            bnormal++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            data.element("label", appList.get(i).getName());
            data1.element("value", btotal);
            data2.element("value", bnormal);
            data3.element("value", btotal - bnormal);
            jname.add(data);
            jtotal.add(data1);
            jnormal.add(data2);
            ja.add(data3);
        }
        obj1.element("palette", "1");
        //obj1.element("xaxisname", "系统名称");
        //obj1.element("yaxisname","数量");
        //obj1.element("numdivlines","9");
        //obj1.element("caption","应用系统健康报表");
        obj2.element("font", "Arial");
        obj2.element("category", jname);
        obj3.element("seriesname", "总数");
        obj3.element("color", "8BBA00");
        obj3.element("data", jtotal);
        obj4.element("seriesname", "正常");
        obj4.element("color", "33ffff");
        obj4.element("data", jnormal);
        obj5.element("seriesname", "异常");
        obj5.element("color", "ff33cc");
        obj5.element("data", ja);
        obj.add(obj3);
        obj.add(obj4);
        obj.add(obj5);

        json.element("chart", obj1);
        json.element("categories", obj2);
        json.element("dataset", obj);

        Struts2Utils.renderText(json.toString());
        return null;
    }

    @Override
    public Object getModel() {
        return null;
    }

    @Override
    public String list() throws Exception {
        return null;
    }

    @Override
    public String input() throws Exception {
        return null;
    }

    @Override
    public String save() throws Exception {
        return null;
    }

    @Override
    public String delete() throws Exception {
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {

    }

}